<?php

namespace backend\controllers;

use Yii;
use common\models\Nav;
use yii\base\ErrorException;
use yii\data\Pagination;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

class NavController extends DashboardController
{
    # 导航首页
    public function actionIndex()
    {
        $data = Nav::getAllNav([]);
        return $this->render('index',compact('data'));
    }

    # 创建 | 编辑页面
    public function actionAdd()
    {
        $id = Yii::$app->request->get('id','');
        $data = empty($id) ? [] : Nav::find()->where(['id' => $id])->asArray()->one();
        return $this->render('add', ['model' => $data]);
    }

    # 更新
    public function actionSave()
    {
        $post = Yii::$app->request->post();
        $item = isset($post['id']) && !empty($post['id']) ? Nav::find()->where(['id' => $post['id']])->one() : new Nav();

        $item->desc = $post['desc'];
        $item->url = $post['url'];
        $item->title = $post['title'];
        $item->status = isset($post['status'])?$post['status']:0;
            
        if ($item->validate() && $item->save()){
            $this->addFlash('success',"恭喜小主，操作成功！");
            return $this->redirect('index');
        } else{
            $this->addFlash('error','验证失败');
        }
    }

    # 删除
    public function actionDelete()
    {
        $id = Yii::$app->request->get('id');
        $model = new Nav();
        if($item = $model->find()->where(['id' => $id])->one()){
            $transaction = Yii::$app->db->beginTransaction();
            //$item->removeAssignments();
            if($item->delete()){
                $transaction->commit();
                $this->ajaxReturn([
                    'state' => 1,
                    'message' => '删除成功！',
                ]);
            }
            $transaction->rollBack();
            $this->ajaxReturn([
                'state' => 0,
                'message' => '删除失败！'.implode('',array_flatten($item->getErrors())),
            ]);
        }else{
            $this->ajaxReturn([
                'state' => 0,
                'message' => '数据不存在！',
            ]);
        }
    }
    /**
     * 导航状态更改
     */

    public function actionChange()
    {
        $id = Yii::$app->request->get('id');
        $model = new Nav();
        if($item = $model->find()->where(['id' => $id])->one()){
            $item->status = $item->status == 1 ? 0 : 1;
            if($item->save()){
                $this->ajaxReturn([
                    'state' => 1,
                    'message' => '切换成功！',
                ]);
            }
            $this->ajaxReturn([
                'state' => 0,
                'message' => '切换失败！'.implode('',array_flatten($item->getErrors())),
            ]);
        }else{
            $this->ajaxReturn([
                'state' => 0,
                'message' => '数据不存在！',
            ]);
        }
    }
    /**
     * Finds the Nav model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Nav the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Nav::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
